 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.PositionsListResponse;
 import com.oanda.blackberry.restapi.Quote;
 import com.oanda.blackberry.restapi.Rate;
 import com.oanda.blackberry.restapi.Symbol;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.Settings;
 import com.oanda.blackberry.util.StringUtils;
 import net.rim.device.api.system.Bitmap;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Graphics;
 import net.rim.device.api.ui.component.ListField;
 
 public class PositionsListField
 {
   private PositionsListResponse[] listData;
   private ListField listField;
   private ListCallBack listFieldCallBack = null;
   int[] quoteState;
   static final int QUOTE_NOCHANGE = 0;
   static final int QUOTE_RIZING = 1;
   static final int QUOTE_REDUCING = 2;
 
   public PositionsListField(PositionsListResponse[] positionsList)
   {
     this.listFieldCallBack = new ListCallBack();
 
     this.listField = new ListField() {
       protected void drawFocus(Graphics graphics, boolean on) {
       }
 
       protected int moveFocus(int amount, int status, int time) {
         int ret = super.moveFocus(amount, status, time);
         invalidate();
         return ret;
       }
 
       protected void moveFocus(int x, int y, int status, int time) {
         int oldSelection = getSelectedIndex();
         super.moveFocus(x, y, status, time);
         int newSelection = getSelectedIndex();
 
         if (oldSelection != -1) {
           invalidate(oldSelection);
         }
 
         if (newSelection != -1) {
           invalidate(newSelection);
         } else {
           setSelectedIndex(oldSelection);
           invalidate(oldSelection);
         }
       }
 
       protected void onDisplay() {
         super.onDisplay();
       }
 
       protected void onUndisplay() {
         super.onUndisplay();
       }
     };
     this.listField.setEmptyString("No data!", 6);
     this.listData = positionsList;
     this.listField.setRowHeight(48);
 
     this.listField.setCallback(this.listFieldCallBack);
 
     int elementLength = positionsList.length;
     this.quoteState = new int[elementLength];
     for (int count = 0; count < elementLength; count++) {
       this.listField.insert(count);
 
       this.quoteState[count] = 0;
     }
   }
 
   public ListField getList()
   {
     return this.listField;
   }
 
   public PositionsListResponse getTradeListSelected() {
     int index = this.listField.getSelectedIndex();
 
     PositionsListResponse data = this.listData[index];
 
     return data;
   }
 
   public PositionsListResponse[] getPositionsListResponses() {
     return this.listData; } 
   private class ListCallBack extends AbstractListFieldCallBack {
 
 
     public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) {
       PositionsListResponse currentRow = (PositionsListResponse)get(list, index);
       Quote currentRowQuote = Model.getModel().getSettings().getRate().getQuoteFromAll(currentRow.getSymbol());
 
       Bitmap icon = null;
 
       Font originalFont = graphics.getFont();
       int originalColor = graphics.getColor();
       int height = list.getRowHeight();
 
       height -= 6;
       w -= 10;
 
       drawBackground(graphics, 5, y, w, height, PositionsListField.this.listField.getSelectedIndex() == index);
       drawBorder(graphics, 5, y, w, height);
 
       Symbol symbol = Model.getModel().getSymbol(currentRow.getSymbol());
       String profit = "";
       double prof = 0.0D;
       int precious = -1;
       if ((symbol != null) && (currentRowQuote != null)) {
         precious = symbol.getPrecision();
 
         if (currentRow.getDir().equalsIgnoreCase("L")) {
           prof = (currentRowQuote.getBid() - currentRow.getAvgPrice()) / symbol.getPiploc();
           profit = StringUtils.formatDouble(prof, precious);
         } else if (currentRow.getDir().equalsIgnoreCase("S")) {
           prof = (currentRow.getAvgPrice() - currentRowQuote.getAsk()) / symbol.getPiploc();
           profit = StringUtils.formatDouble(prof, precious);
         }
 
         String bid = StringUtils.formatDouble(currentRowQuote.getBid(), precious);
         String ask = StringUtils.formatDouble(currentRowQuote.getAsk(), precious);
 
         graphics.setFont(this.normal);
         graphics.drawText(bid + "/" + ask, 250, y, 53, 150);
       }
 
       graphics.setFont(this.normal);
       graphics.drawText(currentRow.getSymbol(), 7, y);
 
       graphics.setFont(this.normal);
       graphics.drawText(currentRow.getDir() + "(" + profit + ")  " + Integer.toString(currentRow.getUnits()) + " @ " + Double.toString(currentRow.getAvgPrice()), 7, y + this.normal.getHeight() + 2);
 
       graphics.setFont(originalFont);
       graphics.setColor(originalColor);
     }
 
     protected void drawBorder(Graphics graphics, int x, int y, int width, int height)
     {
       graphics.setColor(8421504);
       graphics.drawLine(x - 1, y, x + width - 1, y);
       graphics.drawLine(x - 1, y, x - 1, y + height - 1);
 
       graphics.drawLine(x + width, y - 1, x + width, y + height - 1);
       graphics.drawLine(x - 1, y + height - 1, x + width - 1, y + height - 1);
     }
 
     public Object get(ListField list, int index) {
       return PositionsListField.this.listData[index];
     }
 

   }
}

